*     ***************************************************************** *;
*     ***************************************************************** *;
*       File-Name:      replication_TPM.do                              *;
*       Date:           8/02/2018                                       *;
*       Author:         Ben Ferland                                     *;
*       Purpose:        Replication Adams et al. 2006   				*;
*       Input File:     AJPS2006.dta                                    *;
*       Output File:    n.a.			                                *;
*       Data Output:    n.a.     					                    *;
*       Previous file:  n.a.		                                    *;
*       Machine:        MacBook Air Ben                                 *;
*                                                                       *;
*       Details:                									  	*;
*	PART 1 is the replication file provided by Adams et al. 			*;
*	PART 2 is the code for replicating the results of TPM article		*;
*                                                                       *;
*     ****************************************************************  *;
*     ****************************************************************  *;

// Change directory
cd "/Users/Ben/Dropbox/Research projects/interaction/articles/TPM/replication/"


*     ****************************************************************  *;
*     ****************************************************************  *;
*                    	         PART 1                                 *;
*     ****************************************************************  *;
*     ****************************************************************  *;

/***************************************************/
/* 	Replication File for Adams et al. 2006 		   */
/***************************************************/



#delimit;
set matsize 400;

use AJPS2006;

iis partnum;

tis electnum;

egen clnum = group(ctrynum electnum);

/***********************************************************************/
/* Generate variables -- not all of these are used in the final models */
/***********************************************************************/

generate pshift2 = (partyshift2 * (9/200));
generate pshiftt12 = (partyshiftprev * (9/200));
generate pshiftt22 = (partyshiftminustwo * (9/200));

generate pshift2a = abs(pshift2);

generate vshift = vshiftidparty + vshiftmsparty;

generate idvshift = idparty * vshift;

generate idpshift = idparty * pshift2a;

generate msparty = 0;
replace msparty = 1 if idparty==0;

generate msvshift = msparty * vshift;

generate dir = 0;
replace dir = 1 if leftwingparty==1;
replace dir = -1 if rightwingparty==1;

generate centpart = 0;
replace centpart = 1 if dir==0;

generate cconverge = centpart * diffANCPun;

generate dirpshift = dir * pshift2;

generate moderate = 0;
replace moderate = 1 if dirpshift>0;
/*replace moderate = -1 if dirpshift<0;*/

generate modid = moderate * idparty;
generate pshiftmod = moderate * pshift2a;
generate idpshiftmod = moderate * pshift2a * idparty;

generate iddpshift = idparty * dirpshift;

generate dir2 = 0;
replace dir2 = -1 if leftwingparty==1;
replace dir2 = 1 if rightwingparty==1;

generate dirvshift = dir2 * vshift;

sort clnum;

by clnum: generate lvotec = ln(votecurrent);
by clnum: generate lvotec1 = ln(voteminusone);
by clnum: generate lvotec2 = ln(voteminustwo);

by clnum: generate votec = votecurrent - voteminusone;
by clnum: generate votec1 = voteminusone - voteminustwo;

by clnum: egen converge = sum(pshiftmod);
/*replace converge = converge * unweighted_AN;*/

generate idconverge = idparty * converge;

generate lvgain = lvotec - lvotec1;
generate lvgainone = lvotec1 - lvotec2;

generate cgovunemp = ingovnow * changeunemp3;
generate cgovgdp = ingovnow * changegdp3;

generate federal = 0;
replace federal = 1 if fedundim>0;

generate logmag = ln(magnitude);

/* these just pick up Communists in Spain */
generate idfederal = idparty * federal;
generate idlogmag = idparty * logmag;

by clnum: generate numgov = sum(ingovnow);

generate coalgov = 0;
replace coalgov = 1 if numgov > 1;

generate ccogovun = changeunemp3 * coalgov;
generate ccogovgdp = changegdp3 * coalgov;

generate Gunemp = Green * changeunemp3;

generate pshiftAN = unweighted_AN * pshift2a;
generate vshiftAN = unweighted_AN * vshift;

generate dirid = dir * idparty;

generate pvoteshift = votec1 * pshiftt12;

/*****************************/
/******** Table 1 ************/
/*****************************/

tsset partnum electnum;

regress pshift2 vshift idparty idvshift pshiftt12 votec1 pvoteshift 
Italy Britain Greece Luxembourg Denmark Netherlands Spain, 
cluster(clnum);

/* Lagrange Multiplier test for serial correlation */

predict e, resid;
generate lage = L.e;

regress e lage vshift idparty idvshift votec1 pshiftt12 pvoteshift 
Italy Britain Greece Luxembourg Denmark Netherlands Spain;


/**************************************/
/************** Table 2 ***************/
/**************************************/

generate wingconv = wing * converge;

generate modshft = 0;
replace modshft = pshift2a if dirpshift>0;
generate extshft = 0;
replace extshft = pshift2a if dirpshift<0;

generate idmod = idparty * modshft;
generate idext = idparty * extshft;

regress votec modshft extshft idparty idmod idext 
dirvshift converge wing wingconv ingovnow coalgov 
changeunemp3 changegdp3 cgovunemp cgovgdp 
votec1 
Italy Britain Greece Luxembourg Denmark Netherlands Spain
if dirpshift!=0, cluster(clnum);

/* Lagrange Multiplier test for serial correlation */

predict e2, resid;
generate lage2 = L.e2;

regress e2 lage2 votec1 modshft extshft idparty idmod idext 
dirvshift ingovnow coalgov changeunemp3 changegdp3 cgovunemp cgovgdp 
converge wing wingconv Italy Britain Greece Luxembourg Denmark Netherlands Spain
if dirpshift!=0;

#delimit cr




*     ****************************************************************  *;
*     ****************************************************************  *;
*                    	         PART 2                                 *;
*     ****************************************************************  *;
*     ****************************************************************  *;

/***************************************************/
/* 	Replication File for TPM article			   */
/***************************************************/

/**************************************/
/************** Table 1 ***************/
/**************************************/

label variable vshift "Public opinion shift"
label variable idparty "Niche party"
label variable idvshift "Public opinion shift X niche party"
label variable pshiftt12 "Previous policy shift"
label variable votec1 "Previous change in vote share"
label variable pvoteshift "Previous policy shift X previous change in vote share"

///standard approach
regress pshift2 vshift idparty idvshift pshiftt12 votec1 pvoteshift Italy Britain Greece Luxembourg Denmark Netherlands Spain, cluster(clnum)
eststo m1

lincom _b[vshift]+_b[idvshift]

///alternative approach
gen idvshift1=vshift*idparty
label variable idvshift1 "Public opinion shift - niche party"

gen idvshift0=vshift*(1-idparty)
label variable idvshift0 "Public opinion shift - mainstream party"

regress pshift2 idparty idvshift1 idvshift0 pshiftt12 votec1 pvoteshift Italy Britain Greece Luxembourg Denmark Netherlands Spain, cluster(clnum)
eststo m2

lincom _b[idvshift1]-_b[idvshift0]
test _b[idvshift1]=_b[idvshift0]

#delimit;
esttab m1 m2 using "table1.tex",
 nodep staraux star(* 0.10 ** 0.05 *** 0.01) wide se(2) b(2) r2 label
 replace keep(vshift idparty idvshift idvshift1 idvshift0 pshiftt12 votec1 pvoteshift)
 order(vshift idvshift idvshift1 idvshift0 idparty pshiftt12 votec1 pvoteshift) gaps
 mtitles("Standard" "Alternative") alignment(cl) onecell nonotes addnotes(Standard errors in parentheses. *p<0.10, **p<0.05, ***p<0.01. Country dummies are not displayed.)
 title(Explaining Parties' Policy Shifts \label{tab1}); 
#delimit cr
